package parser;

import antlr.RecognitionException;
import antlr.Token;

class Index extends Expr {
    
    Expr left;
    Expr args;
    
    Index(Expr left,  Expr args) {
        this.left = left;
        this.args = args;
    }
    
    void write_get(ByteStream out) {
        //out.write(Opcodes.SWAP_LOAD_SPECIAL);
        //out.write(0);
        //out.write(StandardNames.__getitem__);
        //out.write(Opcodes.SWAP_CALL_SIMPLE);
        //out.write(1);
        out.write(Opcodes.GETITEM);
    }
    
    void write_set(ByteStream out) {
        //out.write(Opcodes.SWAP_LOAD_SPECIAL);
        //out.write(0);
        //out.write(StandardNames.__setitem__);
        //out.write(Opcodes.FLIP_CALL_SIMPLE);
        //out.write(2);
        //out.write(Opcodes.DROP);
        out.write(Opcodes.SETITEM);
    }
    
    void write(ByteStream out) throws RecognitionException {
        left.write(out);
        args.write(out);
        write_get(out);
    }
    
    void writeLHS(ByteStream out) throws RecognitionException {
        left.write(out);
        args.write(out);
        write_set(out);
    }
    
    void writeAugAsgn(int op, Expr value, ByteStream out) throws RecognitionException {
        left.write(out);    // l
        args.write(out); // l a
        out.write(Opcodes.TWO_COPY); // l a l a
        write_get(out);
        value.write(out);   // l a l[a] v
        out.write(Opcodes.INPLACE);
        out.write(op);   // l a l[a]+v
        out.write(Opcodes.RROT);
        write_set(out);
    }

    void delete(ByteStream out) throws RecognitionException {
        left.write(out);
        args.write(out);
        //out.write(Opcodes.SWAP_LOAD_SPECIAL);
        //out.write(0);
        //out.write(StandardNames.__delitem__);
        //out.write(Opcodes.SWAP_CALL_SIMPLE);
        //out.write(1);
        //out.write(Opcodes.DROP);
        out.write(Opcodes.DELITEM);
    }
 
}
